var emailFlag = false, pwdFlag = false, comPwdFlag = false, checkCodeFlag = false, lookFlag = false;

$(function()
{
	refreshCode(); // 产生验证码
	
	$("#imgCaptcha").click(refreshCode); // 验证码点击刷新
	
	$("#txtMemberEmail").focus();
	$("#txtMemberEmail").blur(checkEmail);
	
	$("#txtMemberPwd").blur(checkPwd);
		
	$("#txtComPwd").blur(checkComPwd);
	
	$("#txtCheckCode").blur(checkCode);
	
	$("#chkLook").blur(checkLook);
	
	$("#btnReg").click(register);
});

// 验证码
refreshCode = function()
{
	$("#txtCheckCode").val("");
	var currentTimes = (new Date()).getTime();
	var codeUrl = "image.jsp?k=" + currentTimes;
	$("#imgCaptcha").attr("src", codeUrl);
}

// 检查邮件
checkEmail = function()
{
	var email = $("#txtMemberEmail").val().trim();
	var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	
	if(reg.test(email))
	{
		emailFlag = true;
		$("#spMemberEmail").text("");
		
		checkState();
	}
	else
	{
		$("#spMemberEmail").text("请输入正确的电子邮箱");
	}
}

// 检查密码
checkPwd = function()
{
	var pwd = $("#txtMemberPwd").val().trim();
	var reg = /^\w+$/;
	
	if(reg.test(pwd) && pwd.length >= 6 && pwd.length <= 20)
	{
		pwdFlag = true;
		$("#spMemberPwd").text("");
		
		checkState();
	}
	else
	{
		$("#spMemberPwd").text("请输入6-20位的密码");
	}
}

// 检查确认密码
checkComPwd = function()
{
	var comPwd = $("#txtComPwd").val().trim();
	var pwd = $("#txtMemberPwd").val().trim();
	
	if(comPwd == pwd)
	{
		comPwdFlag = true;
		$("#spComPwd").text("");
		
		checkState();
	}
	else
	{
		$("#spComPwd").text("两次密码输入不一致");
	}
}

// 检查验证码是否为空
checkCode = function()
{
	var code = $("#txtCheckCode").val().trim();
	
	if(code)
	{
		checkCodeFlag = true;
		$("#spCheckCode").text("");
		
		checkState();
	}
	else
	{
		$("#spCheckCode").text("请输入验证码");
	}
}

// 检查协议
checkLook = function()
{
	lookFlag = $("#chkLook").is(":checked");
	
	if(lookFlag)
	{
		checkState();
	}
}

// 检查状态
checkState = function()
{
	if(emailFlag && pwdFlag && comPwdFlag && checkCodeFlag && lookFlag)
	{
		// $("#btnReg").attr("disabled", false);
		$("#btnReg").removeAttr("disabled");
	}
}

// 注册按钮
register = function()
{
	var email = $("#txtMemberEmail").val().trim();
	var gender = $(":radio:checked").val(); // $("input[name=radMemberGender]:checked").val();
	var pwd = $("#txtMemberPwd").val().trim();
	var code = $("#txtCheckCode").val().trim();
	
	var args = {memberEmail : email, memberPwd : pwd, memberGender : gender, code : code};
	var param = {reqData : args, reqUrl : "register"};
	
	ajaxRequest(param, function(returnData)
	{
		if(returnData.hasMember == "true")
		{
			$("#spMemberEmail").text("该邮箱账号已被注册");
		}
		
		if(returnData.codeError == "true")
		{
			$("#spCheckCode").text("验证码输入错误");
		}
		
		if(returnData.add == "true")
		{
			
			alert("注册成功！");
			window.location = "login.jsp?json=" + $.toJSON(json);
		}
	}, null);
}